package com.itheima.health.service.impl;

import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.pojo.OrderSettingDto;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author spt
 * @description
 * @date 2019/9/29
 **/
@Service
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;


//    @Override
//    public boolean add(OrderSetting orderSetting) {
//        return orderSettingDao.add(orderSetting);
//    }

    //批量设置预约人数
    @Override
    public void addBatch(List<ArrayList<String>> sheetData) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        for (int i = 0; i < sheetData.size(); i++) {
            ArrayList<String> data = sheetData.get(i);
            OrderSetting orderSetting = new OrderSetting();
            try {
                orderSetting.setOrderDate(sdf.parse(data.get(0)));//parse：把string转换为date类型
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            orderSetting.setNumber(Integer.parseInt(data.get(1)));

            Long count = orderSettingDao.countNumberExist(orderSetting.getOrderDate());
            if (count>0){//已经设置人数 进行更新
                orderSettingDao.update(orderSetting);
            }
            System.out.println("===========================================================");
            System.out.println(orderSetting);
            orderSettingDao.add(orderSetting);
        }
    }


    @Override
    public List<Map<String, Object>> getOrderSettingByMonth(int year, int month) {
        String time = year + "-" + month;
//            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//            Date begindate = format.parse(begintime);
//            Date enddate = format.parse(endtime);
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        List<OrderSetting> orderSettings = orderSettingDao.selectbydate(time);
        for (OrderSetting orderSetting : orderSettings) {
            Map<String, Object> map = new HashMap<>();
            map.put("date", String.format("%td", orderSetting.getOrderDate()));
            map.put("number", orderSetting.getNumber());
            map.put("reservations", orderSetting.getReservations());
            list.add(map);
        }
        return list;
    }

    //设置每日预约人数
    @Transactional
    @Override
    public void editNumber(OrderSetting orderSetting) {

        log.info("每日预约人数设置:{}",orderSetting);
        //更新每日数据
        //判断当前日期是否已经设置人数
        Long count = orderSettingDao.countNumberExist(orderSetting.getOrderDate());
        if (count>0){//已经设置人数 进行更新
            orderSettingDao.update(orderSetting);
        }
        //当前日期没有设置人数 进行新增
        orderSettingDao.insert(orderSetting);


    }


    /**
     * 查询该日期的预约信息表单数据
     * @return
     */
    @Override
    public OrderSetting selectIdTotal(Date orderDate) {
        OrderSetting orderSetting1= orderSettingDao.selectIdTotal(orderDate);
        return orderSetting1;
    }

    /**
     * 添加当天已完成预约人数
     * @param orderSetting
     */
    @Override
    public void increaseReservations(OrderSetting orderSetting) {
        orderSettingDao.increaseReservations(orderSetting);
    }
}
